//
//  UserDefaults+Extension.swift
//  8btc
//
//  Created by Steven Xie on 2018/7/23.
//  Copyright © 2018年 Hangzhou Shichuo Information Technology Co, Ltd. All rights reserved.
//

import UIKit

public extension UserDefaults {
    /// 判断第一次启动
    ///
    /// - Parameter hasBeenLaunched: UserDefaults 关键词
    /// - Returns: 是否第一次启动
    static func isFirstLaunch(hasBeenLaunched: String) -> Bool {
        let isFirstLaunch = !UserDefaults.standard.bool(forKey: hasBeenLaunched)
        if isFirstLaunch {
            UserDefaults.standard.set(true, forKey: hasBeenLaunched)
            UserDefaults.standard.synchronize()
        }
        return isFirstLaunch
    }

    /// 判断当前版本第一次启动
    ///
    /// - Returns: 是否这个版本第一次启动
    static func isFirstLaunchOfNewVersion() -> Bool {
        // 主程序版本号
        let infoDictionary = Bundle.main.infoDictionary!
        let majorVersion = infoDictionary["CFBundleShortVersionString"] as! String

        // 上次启动的版本号
        let hasBeenLaunchedOfNewVersion = "hasBeenLaunchedOfNewVersion"
        let lastLaunchVersion = UserDefaults.standard.string(forKey:
            hasBeenLaunchedOfNewVersion)

        // 版本号比较
        let isFirstLaunchOfNewVersion = majorVersion != lastLaunchVersion
        if isFirstLaunchOfNewVersion {
            UserDefaults.standard.set(majorVersion, forKey:
                hasBeenLaunchedOfNewVersion)
            UserDefaults.standard.synchronize()
        }
        return isFirstLaunchOfNewVersion
    }
}
